function formatNumber(n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}
export default {
  // 切换tabbar
  setTabBar(role) {
    if (role == 2) {
      // wx.setNavigationBarTitle({
      //   title: '北极星Ai学生端'
      // })
      wx.setTabBarItem({
        index: 2,
        text: "",
        iconPath: "/static/img/Scan.png",
        selectedIconPath: "/static/img/Scan.png",
        complete: res => {
          console.log("变更底部导航2", role);
        }
      });
    } else if (role == 1) {
      // wx.setNavigationBarTitle({
      //   title: '北极星Ai老师端'
      // })
      wx.setTabBarItem({
        index: 2,
        text: "",
        iconPath: "/static/img/more.png",
        selectedIconPath: "/static/img/more.png",
        complete: res => {
          console.log("变更底部导航1", role);
        }
      });
    }
  },
  // 初始化实例数据
  initPage(_this) {
    if (_this.oldData) {
      Object.assign(_this, _this.oldData);
    } else {
      _this.oldData = JSON.parse(JSON.stringify(_this._data));
    }
  },
  // 菜单、层级目录数据格式处理
  menuListHandler(params) {
    let arr = []; //按顺序存储每一项
    let obj = {}; //按self_code存储arr的index
    params.forEach((item, index) => {
      item.level = item.knowledge_level
      item.allChildrenId = item.id + ''
      if (item.knowledge_level == 2) {
        item.child = []
        obj[item.self_code] = arr.length;
        arr.push(item)

      } else if (item.knowledge_level > 2) {

        let arr_index = []
        for (let i = 0; i < item.knowledge_level - 2; i++) {
          let for_arr = item.self_code.split("!");
          var perix = for_arr[0] + '!' + for_arr[1];
          for (let j = 0; j < i; j++) {
            perix += '!' + for_arr[2 + j];
          }
          arr_index.push(obj[perix])
        }
        arr[arr_index[0]].allChildrenId += ',' + item.id
        let temp = arr[arr_index[0]].child
        // console.log()
        for (let i = 1; i < arr_index.length; i++) {
          temp[arr_index[i]].allChildrenId += ',' + item.id
          temp = temp[arr_index[i]].child
        }
        item.child = []
        obj[item.self_code] = temp.length;
        temp.push(item)
      }
    });
    console.log('=====================', arr)
    return arr
  },
  /**
   * 字段长度排序
   */
  sortData(params, str, isDefault) {
    if (params) {
      try {
        // 默认为从小到大
        if (isDefault) {
          return params.sort((a, b) => {
            return a[str].length - b[str].length
          })
        }
        return params.sort((a, b) => {
          return a[str].length - b[str].length
        })
      } catch (error) {
        console.log(error)
        return error
      }
    } else {
      return params
    }
  },
  formatTime(date, isDay) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    const t1 = [year, month, day].map(formatNumber).join('-')
    const t2 = [hour, minute, second].map(formatNumber).join(':')
    if (!isDay) {
      return `${t1} ${t2}`
    } else {
      return `${t1}`
    }
  },
  // 切换unicode
  toUnicode(data, key) {
    switch (data.constructor) {
      case Array:
        data.forEach(item => {
          item[key] = unescape(item[key].replace(/\\u/g, "%u"))
        })
        break;
      case Object:
        data[key] = unescape(data[key].replace(/\\u/g, "%u"))
        break;
      case String:
        data = unescape(data.replace(/\\u/g, "%u"))
        break;

      default:
        break;
    }
    return data
  }
}
